<?php

/**
 * 
 * Enter description here ...
 * @author Administrator
 *
 */
class Lottery_mdl extends CI_Model
{
	
		//表名
	const TBL_LOTTERY = 'cj_lottery';
	public function __construct()
	{
		parent::__construct();
	} 
	
	/**
	 * 取得某个奖项下的所有号码
	 * @param int $rewardid - 奖项ID
	 */
	public function get_lottery_num_by_rewardid($rewardid)
	{
		$lotteryn = array();
		$lotteryn = 
			$this->db->select('lottery.*,user.uid,user.txemail')
			->where('rewardid',$rewardid)
			->from(self::TBL_LOTTERY.' as lottery')
			->join('cj_users as user','user.uid = lottery.uid','left')
			->get()->result_array();
			return $lotteryn;
	}
	
	/**
	 * 购买
	 * @param int rewardid -奖项ID
	 * @param int $rnum - 号码
	 * @param int $uid - 购买者ID
	 */
	public function set_lottery($rewardid,$rnum,$uid)
	{
		//判断奖项是否开始
		$config = array(
			'rewardid' => $rewardid,
			'rnum' =>$rnum
		);
		$data = array(
			'uid' => $uid,
			'createtime' => time()
		);
		return $this->db->update(self::TBL_LOTTERY, $data, $config);
		
	}
	
	/**
	 * 设置奖项
	 * @param int rewardid -奖项ID
	 * @param int $rnum - 号码
	 * @param int $uid - 购买者ID
	 */
	public function set_lottery_rand($rewardid,$rnum,$productid,$stockid)
	{
		$config = array(
			'rewardid' => $rewardid,
			'rnum' =>$rnum
		);
		$data = array(
			'productid' => $productid,
			'stockid' => $stockid
		);
		return $this->db->update(self::TBL_LOTTERY, $data, $config);
		
	}
	
	
	/**
	 * 查看某个号码是否已经被购买
	 * @param int $rewardid - 奖项ID
	 * @param int $rnum - 号码
	 * @return int $uid - 返回购买者ID
	 */
	
	public function get_one_lottery($rewardid,$rnum)
	{
		$lotteryn = array();
		$lotteryn = $this->db->where('rewardid',$rewardid)
						->where('rnum',$rnum)
						->get(self::TBL_LOTTERY)->row();
		
		return $lotteryn;
		
	}
	
	/**
	 * 添加奖项号码
	 * @param  int $id - 奖项ID
	 * @param  array $data - 号码数组
	 */
	
	public function set_lottery_nums($id,$data=array())
	{
		$len = count($data);
		for( $i=0 ; $i<$len ; $i++)
		{
			$d = array(
				'rewardid' => $id,
				'rnum' => $data[$i]
			);
			$this->db->insert(self::TBL_LOTTERY,$d);
		}
	}
	
	/**
	 * 所有号码统计，参与人数，奖品，中奖人
	 * 
	 */
	public function get_lottery_person_product_onther()
	{
		$lotteryn = $this->db->select('lottery.*,product.productname,product.id,users.txusername,users.uid')
						->from(self::TBL_LOTTERY.' as lottery')
						->join('cj_reward as reward','lottery.rewardid = reward.id')
						->join('cj_products as product','product.id = lottery.productid','left')
						->join('cj_users as users','users.uid = lottery.uid')
						//->where('lottery.uid > 0')
						//->where('lottery.productid > 0')
						->get()
						->result_array();
		$flag = array();
		$lottery = array();
		$pnum = array();
		if(!empty($lotteryn))
		{
			foreach($lotteryn as $key => $value)
			{
				$lottery[$value['rewardid']][] = $value;
				$pnum[$value['rewardid']][]=intval($value['uid']);
			}
		}
		$flag['lottery'] = $lottery;
		$flag['pnum'] = $pnum;
		return $flag;
	}
	
	
	
	/*
	 * 
	 * 
	 */
	
	public function del_rewardnum_by_rewardid($id)
	{
		$this->db->where('rewardid', $id);
		$this->db->delete(self::TBL_LOTTERY); 
	}
	
	
	
}